package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.salesmanage.util.Industry;
import com.turing.post.salesmanage.util.IndustryDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "salesmanage.impl.IndustryDaoImpl")
@Remote
@Local
public class IndustryDaoImpl implements IndustryDao {

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public Industry getIndustryByIndustryId(BigInteger industryId) {
		return _em.find(Industry.class, industryId);
	}

	@Override
	public void insertIndustry(Industry industry)
			throws PCPSApplicationException {
		industry.setIndustryId(pcpsCommonDaoImpl.getUniqueIntegerKey());
		GeneralDao.insert(_em, industry);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Industry> getFirstIndustryList() {
		String sql = "select t.industryid,'2'||t.industryno industryno,t.superindustryno,t.industryname,t.remark from industry t where  length(t.industryno)=1 order by t.industryno";
		Query query = _em.createNativeQuery(sql, Industry.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Industry> getIndustryList(String superindustryno) {
		String sql = "select t.* from Industry t where t.superindustryno=?1 order by t.industryno";
		Query query = _em.createNativeQuery(sql, Industry.class);
		query.setParameter(1, superindustryno);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Industry getIndustryByIndustryNo(String IndustryNo) {
		String sql = "select * from Industry where IndustryNo = ?1";
		Query query = _em.createNativeQuery(sql, Industry.class);
		query.setParameter(1, IndustryNo);
		List<Industry> list = query.getResultList();
		if (list == null || list.size() < 1) {
			return null;
		}
		return list.get(0);
	}

}
